package cn.mesmile.juc.list;

import cn.hutool.core.util.RandomUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

/**
 * @author zb
 * @date 2021/8/15 22:13
 * @Description
 */
public class ThreadListDemo {

    public static void main(String[] args) {
        // ArrayList 线程不安全
        // List<Object> objects = new ArrayList<>();
        // 线程安全 Vector
        // List<Object> objects = new Vector<>();
        // 线程安全的  synchronizedList  package java.util
        // List<Object> objects = Collections.synchronizedList(new ArrayList<>());
        // CopyOnWriteArrayList  线程安全 package java.util.concurrent;
        CopyOnWriteArrayList<Object> objects = new CopyOnWriteArrayList<>();
        for (int i = 0; i < 20; i++) {
            new Thread(() ->{
                // 添加
                objects.add(RandomUtil.randomString(6));
                // 打印
                System.out.println(Thread.currentThread().getName()+" objects = " + objects);
            },"thread-"+i).start();
        }
    }
}
